<html>
<head><meta charset="utf-8"><title>struct conditionally implementing Drop/Copy · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/struct.20conditionally.20implementing.20Drop.2FCopy.html">struct conditionally implementing Drop/Copy</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="207071118"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/struct%20conditionally%20implementing%20Drop/Copy/near/207071118" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/struct.20conditionally.20implementing.20Drop.2FCopy.html#207071118">(Aug 16 2020 at 13:09)</a>:</h4>
<p>I'm working on a problem where I want a single <code>struct</code> to be both Copy and Drop (though never at the same time), based on a generic parameter implementing Copy.</p>
<p>The straightforward definition doesn't work, because rustc complains about both Copy and Drop being implemented. AFAICT, the only way to bypass this today is to have two separate struct definitions, which is rather a pain. Could we relax this restriction?</p>
<div class="codehilite"><pre><span></span><code><span class="k">struct</span> <span class="nc">Bar</span><span class="o">&lt;</span><span class="n">P</span><span class="o">&gt;</span><span class="p">(</span><span class="n">P</span><span class="p">);</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">P</span>: <span class="nb">Copy</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Copy</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Bar</span><span class="o">&lt;</span><span class="n">P</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">P</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Drop</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Bar</span><span class="o">&lt;</span><span class="n">P</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">..</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="207071162"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/struct%20conditionally%20implementing%20Drop/Copy/near/207071162" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/struct.20conditionally.20implementing.20Drop.2FCopy.html#207071162">(Aug 16 2020 at 13:10)</a>:</h4>
<p>I'd be fine with something like <code>impl&lt;P: !Copy&gt; Drop for Bar&lt;P&gt; {}</code> to be clear</p>



<a name="207086981"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/struct%20conditionally%20implementing%20Drop/Copy/near/207086981" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/struct.20conditionally.20implementing.20Drop.2FCopy.html#207086981">(Aug 16 2020 at 19:58)</a>:</h4>
<p>Right now, you can't constrain <code>Drop</code> differently than the type at all.</p>



<a name="207086988"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/struct%20conditionally%20implementing%20Drop/Copy/near/207086988" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/struct.20conditionally.20implementing.20Drop.2FCopy.html#207086988">(Aug 16 2020 at 19:59)</a>:</h4>
<p>i.e. <code>Drop</code> is all or nothing, nevermind what you want to do with <code>Copy</code></p>



<a name="207087377"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/struct%20conditionally%20implementing%20Drop/Copy/near/207087377" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/struct.20conditionally.20implementing.20Drop.2FCopy.html#207087377">(Aug 16 2020 at 20:05)</a>:</h4>
<p>Recent thread: <a href="https://internals.rust-lang.org/t/can-we-fix-drop-to-allow-specialization/12873">https://internals.rust-lang.org/t/can-we-fix-drop-to-allow-specialization/12873</a></p>



<a name="207088043"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/struct%20conditionally%20implementing%20Drop/Copy/near/207088043" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/struct.20conditionally.20implementing.20Drop.2FCopy.html#207088043">(Aug 16 2020 at 20:19)</a>:</h4>
<p>Hm yes, that is indeed recent. I don't think that thread answers whether we can just allow Copy and Drop to co-exist (perhaps with a warning)</p>



<a name="207088053"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/struct%20conditionally%20implementing%20Drop/Copy/near/207088053" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/struct.20conditionally.20implementing.20Drop.2FCopy.html#207088053">(Aug 16 2020 at 20:19)</a>:</h4>
<p>in my case the drop impl would then be a no-op, semantically at least, since it would just do mem::drop on a copy value</p>



<a name="207088175"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/struct%20conditionally%20implementing%20Drop/Copy/near/207088175" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/struct.20conditionally.20implementing.20Drop.2FCopy.html#207088175">(Aug 16 2020 at 20:22)</a>:</h4>
<p>That seems like it should need more than a warning, since it'd be hard to predict when the Drop will get called and when it won't.</p>



<a name="207088238"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/struct%20conditionally%20implementing%20Drop/Copy/near/207088238" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/struct.20conditionally.20implementing.20Drop.2FCopy.html#207088238">(Aug 16 2020 at 20:24)</a>:</h4>
<p>Well, in my case for Copy-implementing P, the Drop impl is basically just <code>fn drop(&amp;mut self) {}</code> which is fine to not get called <em>ever</em></p>



<a name="207088250"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/struct%20conditionally%20implementing%20Drop/Copy/near/207088250" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/struct.20conditionally.20implementing.20Drop.2FCopy.html#207088250">(Aug 16 2020 at 20:24)</a>:</h4>
<p>I know a number of unsafe authors, and authors in general, who assume that Copy and Drop are fully exclusive to one another, so they use Copy bounds as a !Drop bound.</p>



<a name="207088268"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/struct%20conditionally%20implementing%20Drop/Copy/near/207088268" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/struct.20conditionally.20implementing.20Drop.2FCopy.html#207088268">(Aug 16 2020 at 20:25)</a>:</h4>
<p><span class="user-mention" data-user-id="224471">@Lokathor</span> right, and that's essentially true (you can't have a Drop implementing Copy struct AFACIT, that's what this thread is about) but I don't think what I'm asking about would actually change that -- the Drop impl wouldn't matter</p>



<a name="207088319"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/struct%20conditionally%20implementing%20Drop/Copy/near/207088319" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/struct.20conditionally.20implementing.20Drop.2FCopy.html#207088319">(Aug 16 2020 at 20:26)</a>:</h4>
<p>to be more concrete <a href="https://github.com/rust-lang/rust/pull/75590">https://github.com/rust-lang/rust/pull/75590</a> is where I'm encountering this -- note the essentially duplicated CopyTaggedPtr and TaggedPtr</p>



<a name="207192693"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/struct%20conditionally%20implementing%20Drop/Copy/near/207192693" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/struct.20conditionally.20implementing.20Drop.2FCopy.html#207192693">(Aug 17 2020 at 20:22)</a>:</h4>
<p><span class="user-mention" data-user-id="116122">@simulacrum</span> The most troublesome case I could come up with is this one:</p>
<div class="codehilite"><pre><span></span><code><span class="k">struct</span> <span class="nc">Foo</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="o">&gt;</span><span class="p">(</span><span class="o">&amp;</span><span class="na">&#39;a</span><span class="w"> </span><span class="kt">i32</span><span class="p">);</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="nb">Drop</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Foo</span><span class="o">&lt;</span><span class="nb">&#39;static</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">drop</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;uhoh&quot;</span><span class="p">);</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">bar</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="o">&gt;</span><span class="p">(</span><span class="n">_x</span>: <span class="nc">Foo</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// _x is dropped here; does it need to run code?</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>Basically trying to find a way to encode the problems I remembered from specialization <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>
<p>For things that are monomorphized I couldn't come up with a problem (though maybe one could hide it in a boxed trait object or something somehow and have it again; I didn't think through vtable implications) but anything that's erased seems complex.</p>



<a name="207192893"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/struct%20conditionally%20implementing%20Drop/Copy/near/207192893" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/struct.20conditionally.20implementing.20Drop.2FCopy.html#207192893">(Aug 17 2020 at 20:24)</a>:</h4>
<p>yeah I get that there's ways to break it</p>



<a name="207192907"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/struct%20conditionally%20implementing%20Drop/Copy/near/207192907" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/struct.20conditionally.20implementing.20Drop.2FCopy.html#207192907">(Aug 17 2020 at 20:24)</a>:</h4>
<p>my point is it feels like there should be some escape hatch</p>



<a name="207192935"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/struct%20conditionally%20implementing%20Drop/Copy/near/207192935" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/struct.20conditionally.20implementing.20Drop.2FCopy.html#207192935">(Aug 17 2020 at 20:24)</a>:</h4>
<p>and note that I don't actually want a "conditional impl" in that sense</p>



<a name="207192992"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/struct%20conditionally%20implementing%20Drop/Copy/near/207192992" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/struct.20conditionally.20implementing.20Drop.2FCopy.html#207192992">(Aug 17 2020 at 20:25)</a>:</h4>
<p>i.e., I am 100% fine with Drop and Copy coexisting and drop running (or not running)</p>



<a name="207397685"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/struct%20conditionally%20implementing%20Drop/Copy/near/207397685" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/struct.20conditionally.20implementing.20Drop.2FCopy.html#207397685">(Aug 19 2020 at 11:51)</a>:</h4>
<p>I don't remember exactly <em>why</em> we made <code>Drop</code> be all-or-nothing</p>



<a name="207397716"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/struct%20conditionally%20implementing%20Drop/Copy/near/207397716" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/struct.20conditionally.20implementing.20Drop.2FCopy.html#207397716">(Aug 19 2020 at 11:51)</a>:</h4>
<p>I thnk it had to do both with being cautious but also that Drop is significant to borrow checker and has to be checked at monomorphization time</p>



<a name="207397765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/struct%20conditionally%20implementing%20Drop/Copy/near/207397765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/struct.20conditionally.20implementing.20Drop.2FCopy.html#207397765">(Aug 19 2020 at 11:52)</a>:</h4>
<p>and when we were doing drop elaboration, we didn't want it to be a complex result</p>



<a name="207397782"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/struct%20conditionally%20implementing%20Drop/Copy/near/207397782" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/struct.20conditionally.20implementing.20Drop.2FCopy.html#207397782">(Aug 19 2020 at 11:52)</a>:</h4>
<p>it may be possible to loosen that restriction but I have to say I'd be reluctant to open that can of worms at present :)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>